package com.gcp.freemarker.controller;

import com.gcp.freemarker.dto.InitDto;
import com.gcp.freemarker.service.TableService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/order")
public class TableController {

    @Resource
    private TableService tableService;

    @PostMapping("/initProj")
    public String initProj(@RequestBody InitDto initDto) {
        //生成crm_qw数据库中所有表相关的源代码，并保存在D:/eco11/crm-biz-qw/crm-biz-qw文件夹下
        return tableService.getTableInfo(initDto.getSchemaName(),initDto.getRealPath(),initDto.getTableSubIndex(),initDto.getPackageName());
    }

    @GetMapping("/generateCode")
    public String generateCode(String tableName,String tableComment) {
        //生成m_order表相关的源代码，并保存在本项目的包内
        return tableService.generateCode(tableName, "D:/eco/scrm-xhs",snakeToPascalCase(tableName),"com.eco.xhs",tableComment);
    }

    public static String snakeToPascalCase(String snakeCase) {
        if (snakeCase == null || snakeCase.isEmpty()) {
            return snakeCase;
        }

        StringBuilder result = new StringBuilder();
        String[] words = snakeCase.split("_");

        for (String word : words) {
            if (!word.isEmpty()) {
                result.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    result.append(word.substring(1).toLowerCase());
                }
            }
        }

        return result.toString();
    }

}
